<template>
  <div class="role-container">
    <div class="left-wrapper">
      <div v-for="(item,index) in roleList" :key="item.roleId" class="role-item"
      :class="{active:index==activeIndex}"
       @click="changeIndex(index)">
        <div class="role-info">
          <svg-icon :icon-class="index==activeIndex?'user-active':'user'" class="icon" />
          {{item.roleName}}
        </div>
        <div class="more">
          <el-dropdown>
            <span class="el-dropdown-link">
              <svg-icon icon-class="more" />
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="$router.push(`/addRole?id=${item.roleId}`)">编辑角色</el-dropdown-item>
              <el-dropdown-item @click.native="delRole(item.roleId)">删除</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
      </div>
       <el-button class="addBtn" size="mini" @click="$router.push('/addRole')">添加角色</el-button>
    </div>

    <div class="right-wrapper" >

      <el-tabs v-model="activeName">
        <el-tab-pane label="功能权限" name="tree">
          <div class="tree-wrapper">
            <div v-for="item in treeList" :key="item.id" class="tree-item">
              <div class="tree-title" > {{item.title}} </div>
              <el-tree
              ref="trees"
                :data="item.children"
                :props="defaultProps"
                default-expand-all
                show-checkbox
                node-key="id"
                />
            </div>
          </div>
        </el-tab-pane>
        <el-tab-pane :label="`成员(${total})`" name="user">
          <el-table
      :data="userList"
      style="width: 100%">
      <el-table-column
        prop="id"
        label="序号"
        width="180">
      </el-table-column>
      <el-table-column
        prop="name"
        label="员工姓名"
        width="180">
      </el-table-column>
      <el-table-column
        prop="username"
        label="用户名">
      </el-table-column>
    </el-table>
        </el-tab-pane>
      </el-tabs>




    </div>
  </div>
</template>

<script>
import { getRoleListAPI,getTreeListAPI,getSearchListAPI,getRoleUserAPI,delRoleUserAPI } from '@/api/system';

export default {
  data(){
    return{
      activeName:'tree',
      roleList:[],
      //userList中成员的总数
      total:0,
      userList:[],
      activeIndex:'',
      treeList:'',
      defaultProps:{
        label:'title',
        children:'children',
        disabled:function(){
          return true
        }
      },
      perms:[]
    }
  },
  created(){
    this.getRoleList()
    this.getTreeList()
    // this.getSearchList(2)

  },
  methods:{
    //删除角色
    delRole(roleId) {
  this.$confirm('是否确认删除当前角色?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async() => {
    await delRoleUserAPI(roleId)
    this.getRoleList()
    this.$message({
      type: 'success',
      message: '删除成功!'
    })
  })
},
    // 获取角色下面的用户列表
    async getRoleUserList(roleId){
      const res=await getRoleUserAPI(roleId)
      // console.log(res)
      this.userList=res.data.rows
      this.total=res.data.total
    },
    async getSearchList(roleId){
      const res=await getSearchListAPI(roleId)
      // console.log(res)
      this.perms=res.data.perms
      this.$refs.trees.forEach((tree,index)=>{
        //对每一棵树进行设置
        tree.setCheckedKeys(this.perms[index])
      })
    },
    changeIndex(index){
      // console.log(index)
      this.activeIndex=index
      //获取选中的roleId
      const roleId=this.roleList[index].roleId
      // console.log(roleId);
      this.getSearchList(roleId)
      this.getRoleUserList(roleId)

    },
    async getRoleList(){
      const res=await getRoleListAPI()
      this.roleList=res.data
      // console.log(res)
    },
    async getTreeList(){
      const res=await getTreeListAPI()
      // console.log(res);
      this.treeList=res.data


    }
  }
}
</script>

<style scoped lang="scss">
.role-container {
  display: flex;
  font-size: 14px;
  background-color: #fff;
  padding:20px;
  .left-wrapper {
    width: 200px;
    border-right: 1px solid #e4e7ec;
    padding: 4px;
    text-align: center;

    .role-item {
      position: relative;
      height: 56px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 0 20px;
      cursor: pointer;

      &.active {
        background: #edf5ff;
        color: #4770ff;
      }
    }

    .role-info {
      display: flex;
      align-items: center;

      .icon {
        margin-right: 10px;
      }
    }

    .more {
      display: flex;
      align-items: center;
    }
    .addBtn{
      width: 100%;
      margin-top: 20px;
    }
  }

  .right-wrapper {
    flex: 1;

    .tree-wrapper {
      display: flex;
      justify-content: space-between;

      .tree-item {
        flex: 1;
        border-right: 1px solid #e4e7ec;
        padding: 0px 4px;
        text-align: center;
        .tree-title {
          background: #f5f7fa;
          text-align: center;
          padding: 10px 0;
          margin-bottom: 12px;
        }
      }
    }

    ::v-deep .el-tabs__nav-wrap {
      padding-left: 20px;
    }

    .user-wrapper{
      padding:20px;
    }
  }
}
</style>
